
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.view.*;
import com.huizhan.web.model.*;

/**
 * 项目角色api
 */
 
@Api(tags = "ProjectRole",description = "项目角色")
@RestController
public class ProjectRoleController {
    
    @Autowired
    private ProjectRoleDao dao;
   
    @Autowired
    private ProjectTypeDao daoProjectType;
   
    @Autowired
    private ProjectDao daoProject;

    /**
     * 获取项目角色集合
     * @return api结果
     */
    @ApiOperation(value = "获取项目角色集合")
    @RequestMapping(value =  "/api/ProjectRole/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectRole>> Get(){
         ApiResult<List<ProjectRole>> result=new ApiResult<List<ProjectRole>>();
        try 
        {
            List<ProjectRole> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取项目角色信息
     * @param id 项目角色ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取项目角色信息")
    @RequestMapping(value =  "/api/ProjectRole/{id}",method = {RequestMethod.GET})
    public ApiResult<ProjectRole> Get(@PathVariable Long id){
        ApiResult<ProjectRole> result=new ApiResult<ProjectRole>();
        try
        {
            ProjectRole model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }


    /**
     * 获取项目类型/项目/角色三级树
     * @return api结果
     */
    @ApiOperation(value = "获取项目类型/项目/角色三级树")
    @RequestMapping(value =  "/api/ProjectRole/Tree/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectRoleTree>> Tree(){
        ApiResult<List<ProjectRoleTree>> result=new ApiResult<List<ProjectRoleTree>>();
        try
        {
           List<ProjectType> list= daoProjectType.findAll();
           List<Project> ps= daoProject.findAll();
           List<ProjectRole> roles= dao.findAll();
           List<ProjectRoleTree> data=new ArrayList<ProjectRoleTree>();

            for (ProjectType projectType : list) {
                ProjectRoleTree tree=new ProjectRoleTree(projectType);
                for (Project p : ps) {
                    if(p.getProjectTypeId()==projectType.getProjectTypeId()){
                        ProjectTree tree1=new ProjectTree(p);
                        tree.getChildren().add(tree1);
                        for (ProjectRole role : roles) {
                            if(role.getProjectId()==tree1.getProjectId()){
                                tree1.getChildren().add(role);
                            }
                        }
                    }
                }
                data.add(tree);
            }
            result.setData(data);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目ID获取项目角色集合
     * @param projectId 项目ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取项目角色集合")
    @RequestMapping(value =  "/api/ProjectRole/Project/{projectId}",method = {RequestMethod.GET})
    public ApiResult<List<ProjectRole>> Project(@PathVariable String projectId){
        ApiResult<List<ProjectRole>> result=new ApiResult<List<ProjectRole>>();
        try
        {
            List<ProjectRole> model= dao.findByProjectId(projectId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改项目角色信息
     * @param id 项目角色ID
     * @param model 项目角色信息
     * @return api结果
     */
    @ApiOperation(value = "修改项目角色信息")
    @RequestMapping(value =  "/api/ProjectRole/{id}",method = {RequestMethod.PUT})
    public ApiResult<ProjectRole> Edit(@PathVariable Long id,@RequestBody ProjectRole model){
        ApiResult<ProjectRole> result=new ApiResult<ProjectRole>();
        try
        {
            model.setProjectRoleId(id);
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加项目角色信息
     * @param model 项目角色信息
     * @return api结果
     */
    @ApiOperation(value = "添加项目角色信息")
    @RequestMapping(value =  "/api/ProjectRole/",method = {RequestMethod.POST})
    public ApiResult<ProjectRole> Add(@RequestBody ProjectRole model){
         ApiResult<ProjectRole> result=new ApiResult<ProjectRole>();
        try
        {
            ProjectRole role=dao.findByProjectIdAndName(model.getProjectId(),model.getName());

            if(role!=null){
                result.setHasError(true);
                result.setMessage("该角色名称已经存在");
            }else{
                model.setCreateDate(new Date());
                dao.save(model);
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除项目角色信息
     * @param id 项目角色ID
     * @return api结果
     */
    @ApiOperation(value = "删除项目角色信息")
    @RequestMapping(value =  "/api/ProjectRole/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

